<?php
declare(strict_types=1);

namespace app\controller\merchant;

use app\BaseController;
use app\model\MessageBlacklist;
use think\facade\Log;
use think\Request;

class MessageBlacklistController extends BaseController
{
    /**
     * 获取黑名单列表
     */
    public function index(Request $request)
    {
        try {
            $params = $request->get();
            $merchantId = $request->merchant_id;
            
            $query = MessageBlacklist::where('merchant_id', $merchantId);
            
            // 按消息类型筛选
            if (!empty($params['message_type'])) {
                $query->where('message_type', $params['message_type']);
            }
            
            // 按用户ID筛选
            if (!empty($params['user_id'])) {
                $query->where('user_id', $params['user_id']);
            }
            
            $list = $query->with(['user'])
                ->order('id', 'desc')
                ->paginate($params['limit'] ?? 15);
            
            return json(['code' => 0, 'msg' => '获取成功', 'data' => $list]);
        } catch (\Exception $e) {
            Log::error('获取黑名单列表失败: ' . $e->getMessage());
            return json(['code' => 1, 'msg' => $e->getMessage()]);
        }
    }

    /**
     * 添加黑名单
     */
    public function create(Request $request)
    {
        try {
            $data = $request->post();
            $merchantId = $request->merchant_id;
            
            // 检查是否已存在
            $exists = MessageBlacklist::where([
                'merchant_id' => $merchantId,
                'user_id' => $data['user_id'],
                'message_type' => $data['message_type']
            ])->find();
            
            if ($exists) {
                throw new \Exception('该用户已在黑名单中');
            }
            
            // 创建黑名单记录
            MessageBlacklist::create([
                'merchant_id' => $merchantId,
                'user_id' => $data['user_id'],
                'message_type' => $data['message_type'],
                'reason' => $data['reason'] ?? '',
                'expire_time' => $data['expire_time'] ?? null,
                'create_time' => date('Y-m-d H:i:s'),
                'update_time' => date('Y-m-d H:i:s')
            ]);
            
            return json(['code' => 0, 'msg' => '添加成功']);
        } catch (\Exception $e) {
            Log::error('添加黑名单失败: ' . $e->getMessage());
            return json(['code' => 1, 'msg' => $e->getMessage()]);
        }
    }

    /**
     * 移除黑名单
     */
    public function delete(Request $request)
    {
        try {
            $id = $request->delete('id');
            $merchantId = $request->merchant_id;
            
            // 获取黑名单记录
            $blacklist = MessageBlacklist::where([
                'id' => $id,
                'merchant_id' => $merchantId
            ])->find();
            
            if (!$blacklist) {
                throw new \Exception('记录不存在');
            }
            
            // 删除记录
            $blacklist->delete();
            
            return json(['code' => 0, 'msg' => '删除成功']);
        } catch (\Exception $e) {
            Log::error('删除黑名单失败: ' . $e->getMessage());
            return json(['code' => 1, 'msg' => $e->getMessage()]);
        }
    }
} 